﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    /// <summary>
    /// 命令上下文
    /// </summary>
    public class CommandContext
    {
        private List<string> HistoryView = new List<string>();
        /// <summary>
        /// 当前执行的命令
        /// </summary>
        public string Command { get; set; }
        /// <summary>
        /// 当前视图
        /// </summary>
        public string View { get { return _view; } }
        private string _view = "";
        /// <summary>
        /// 接口视图下，当前的接口
        /// </summary>
        public string Interface { get; set; }
        /// <summary>
        /// 退出到上一级视图
        /// </summary>
        public void ExitView()
        {
            this.HistoryView.RemoveAt(this.HistoryView.Count - 1);
            if (this.HistoryView.Count > 0)
            {
                this._view = this.HistoryView[this.HistoryView.Count - 1];
            }
            else
            {
                System.Environment.Exit(0);
            }
        }
        /// <summary>
        /// 进入到某个视图
        /// </summary>
        public void EnterView(string View)
        {
            HistoryView.Add(View);
            this._view = View;
        }

        /// <summary>
        /// 主机名
        /// </summary>
        public string Hostname { get; set; }
        /// <summary>
        /// 是否处于接口视图
        /// </summary>
        public bool IsIfView { get { return CommandView.COMMAND_VIEW_IF.Equals(View.ToLower()); } }
        /// <summary>
        /// 是否处于全局视图
        /// </summary>
        public bool IsGlobalView { get { return CommandView.COMMAND_VIEW_GLOBAL.Equals(View.ToLower()); } }
        /// <summary>
        /// 用户输入命令解析后的参数，在服务层给真正业务中使用
        /// </summary>
        public Dictionary<string, string> InputCommandParams { get; set; }
        /// <summary>
        /// 头文本
        /// </summary>
        public string HeaderText
        {
            get
            {
                string ext = IsIfView ? $"-{Interface}" : "";
                return $"CNMS {View}{ext}> ";
            }
        }
    }
    public class CommandView
    {
        public static string COMMAND_VIEW_IF = "if";
        public static string COMMAND_VIEW_GLOBAL = "global";
    }
}
